﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace CarInfor.DAL
{
    /// <summary>
    /// DB 类的摘要说明。
    /// 提供访问Access数据库的基本方法	
    /// </summary>
    public class DB
    {
        //private string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Environment.CurrentDirectory + "\\" + "db.accdb";
        string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "\\" + "db.mdb;jet OleDB:Database Password=2011";

        /// <summary>
        /// 获取查询结果
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        public DataSet GetSqlDataSet(string sqlString)
        {
            DataSet ds = new DataSet();
            try
            {
                OleDbConnection sqlConn = new OleDbConnection(connStr);
                OleDbDataAdapter da = new OleDbDataAdapter(sqlString, sqlConn);
                da.Fill(ds);
            }
            catch (Exception e)
            {
                throw (e);
            }
            return ds;
        }


        /// <summary>
        /// 执行一个查询
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        public int ExecuteSql(string sqlString)
        {
            int i = 0;
            try
            {
                OleDbConnection sqlConn = new OleDbConnection(connStr);
                OleDbCommand sqlCmd = new OleDbCommand(sqlString, sqlConn);
                sqlConn.Open();
                i = sqlCmd.ExecuteNonQuery();
                sqlConn.Close();
            }
            catch (Exception e)
            {
                throw (e);
            }
            return i;
        }

    }
}
